package com.sgb.kjwl.view.ui.activity.transport;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.Editable;
import android.util.TypedValue;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.fondesa.recyclerviewdivider.DividerDecoration;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.AppActDriverBindBinding;
import com.sgb.kjwl.model.entity.self.ProprietaryDriverEntity;
import com.sgb.kjwl.model.response.ListResponseV1;
import com.sgb.kjwl.utils.CustomTextWatcher;
import com.sgb.kjwl.view.constant.RouterKey;
import com.sgb.kjwl.view.ui.adapter.transport.DriverBindAdapter;
import com.sgb.kjwl.viewmodel.transport.DriverBindViewModel;

import com.swgk.core.common.base.BaseActivity;
import com.swgk.core.util.ArrayUtil;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 杨畔
 * @date: 2021/10/9 09:59
 * @Description: 司机绑定页
 */
public class DriverBindActivity extends BaseActivity<DriverBindViewModel, AppActDriverBindBinding> {
    //车辆no
    private long mNo;
    //记录列表
    private List<ProprietaryDriverEntity> mList;
    private DriverBindAdapter mAdapter;

    public static void start(Context context, long no) {
        Intent intent = new Intent(context, DriverBindActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra(RouterKey.NO, no);
        context.startActivity(intent);
    }

    @Override
    protected int layoutId() {
        return R.layout.app_act_driver_bind;
    }

    @Override
    protected void createViewModel() {
        mViewModel = new ViewModelProvider(this).get(DriverBindViewModel.class);
    }

    @Override
    protected void init() {
        mBinding.includeTitle.titleView.setText("绑定司机");
        initRecycler();
        initEvent();
        initData();
    }

    private void initRecycler() {
        mList = new ArrayList<>();
        mAdapter = new DriverBindAdapter(mList);
        mBinding.rvDriverBind.setLayoutManager(new LinearLayoutManager(this));
        DividerDecoration.builder(this)
                .color(Color.TRANSPARENT)
                .size(10, TypedValue.COMPLEX_UNIT_DIP)
                .build()
                .addTo(mBinding.rvDriverBind);
        mAdapter.bindToRecyclerView(mBinding.rvDriverBind);
    }

    private void initEvent() {
        mBinding.setOnClick(this);
        mBinding.includeTitle.ivBack.setOnClickListener(this);
        mBinding.srlDriverBind.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
                mViewModel.getDriverList(DriverBindActivity.this, false);
            }

            @Override
            public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
                mViewModel.getDriverList(DriverBindActivity.this, true);
            }
        });
        mBinding.etDriverBindSearch.addTextChangedListener(new CustomTextWatcher() {
            @Override
            protected void delayedChanged(Editable content) {
                mViewModel.mDriverName = content.toString();
            }
        });
        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (view.getId() == R.id.tv_bind) {
                mViewModel.bindDriver(this, mNo, mList.get(position));
            }
        });
        mViewModel.ldList.observe(this, this::syncList);
    }

    private void initData() {
        mNo = getIntent().getLongExtra(RouterKey.NO, 0);
        mViewModel.getDriverList(this, true);
    }

    private void syncList(ListResponseV1<List<ProprietaryDriverEntity>> listResponse) {
        mBinding.srlDriverBind.finishRefresh();
        mBinding.srlDriverBind.finishLoadMore();
        if (!listResponse.loadSuccess()) {
            if (listResponse.getPageNumber() == 1) {
                mAdapter.setEmptyView(R.layout.layout_null);
            }
        }
        if (listResponse.getPageNumber() == 1) {
            mList.clear();
        }
        if (listResponse.getList() != null) {
            mList.addAll(listResponse.getList());
        }
        mAdapter.notifyDataSetChanged();
        //
        mBinding.srlDriverBind.setNoMoreData(!listResponse.haveMoreData());
        //设置空布局
        if (ArrayUtil.isNullOrEmpty(mList)) {
            mAdapter.setEmptyView(R.layout.layout_null);
        }
    }

    @Override
    public void onMultiClick(View v) {
        if (v == mBinding.includeTitle.ivBack) {
            finish();
        } else if (v == mBinding.tvDriverBindSearch) {
            //搜索
            mViewModel.getDriverList(DriverBindActivity.this, true);
        }
    }
}
